package xuhaijia.lanou3g.autohome.forum;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.StringValues;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.forum.forumreusing.ListViewFragment;


/**
 * Created by 7 on 16/8/16.
 */
public class JxRecommendFragment extends BaseFragment implements View.OnClickListener {

    public TabLayout tabLayout;
    private ForumFragment father;
    private String[] url;
    private String[] strs;
    private int position;


    @Override
    protected int setLayout() {
        return R.layout.fragment_jxrecommend;
    }

    @Override
    protected void initView() {
        tabLayout = bindView(R.id.jxrecommend_tb);
        tabLayout.setTabTextColors(Color.BLACK, Color.BLUE);
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                father.setListSelect(pos);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        ImageView menuIv = bindView(R.id.jxrecommend_menu);
        menuIv.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        strs = StringValues.strs;
        url = StringValues.url;
        for (int i = 0; i < strs.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText(strs[i]);
            tabLayout.addTab(tab);
        }
        firstLayout();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                ListViewFragment fragment = new ListViewFragment();
                Bundle bundle = new Bundle();
                bundle.putString("url", url[tab.getPosition()]);
                fragment.setArguments(bundle);
                fragmentTransaction.replace(R.id.jxrecommend_framenlayout, fragment).commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onClick(View v) {
        father.openRightLayout();
    }

    public void setFather(ForumFragment father) {
        this.father = father;
    }

    public void firstLayout() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ListViewFragment fragment = new ListViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", URLValues.FOURM_URL);
        fragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.jxrecommend_framenlayout, fragment).commit();
    }


    public void setLocation(int location) {
        this.position = location;
        tabLayout.setScrollPosition(position,0,true);
        tabLayout.getTabAt(position).select();
    }
}


